package ${packageName}.web;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.common.core.R;
import ${packageName}.dao.${name};
import ${packageName}.dao.${name}Dao;
import ${packageName}.service.${name}Service;
import ${packageName}.vo.${name}AddVo;
import ${packageName}.vo.${name}BatchVo;
import ${packageName}.vo.${name}UpdateVo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import javax.validation.Valid;

/**
* @author zg
*/
@RestController
@RequiredArgsConstructor
public class ${name}Controller {
	private final ${name}Dao ${name?uncap_first}Dao;
	private final ${name}Service ${name?uncap_first}Service;

	@PostMapping("${mapping}/add")
	public R<?> add(@RequestBody @Valid ${name}AddVo param) {
		${name} obj = ${name?uncap_first}Service.add(param);
		return R.success(obj);
	}

	@PostMapping("${mapping}/del")
	public R<?> del(@RequestBody List<Long> idList) {
		${name?uncap_first}Service.del(idList);
		return R.success();
	}

	@PostMapping("${mapping}/update")
		public R<?> update(@RequestBody @Valid ${name}UpdateVo param) {
		${name} obj = ${name?uncap_first}Service.update(param);
		return R.success(obj);
	}

	@PostMapping("${mapping}/batch")
		public R<?> batch(@RequestBody @Valid ${name}BatchVo param) {
		${name?uncap_first}Service.batchUpdate(param);
		return R.success();
	}
<#if hasSortColumn>
	@PostMapping("${mapping}/sort")
	public R<?> sort(@RequestBody String sortStr) {
		${name?uncap_first}Service.sort(sortStr);
		return R.success();
	}
</#if>

	@GetMapping("${mapping}/info")
	public R<${name}> info(Long id) {
		${name} obj = ${name?uncap_first}Dao.getById(id);
		return R.success(obj);
	}

	@GetMapping("${mapping}/all")
	public R<List<${name}>> all() {
		List<${name}> list = ${name?uncap_first}Dao.list();
		return R.success(list);
	}

	@GetMapping("${mapping}/page")
	public R<IPage<${name}>> page(Integer page, Integer pageSize, String searchStr) {
		LambdaQueryWrapper<${name}> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		if (StrUtil.isNotBlank(searchStr)) {
			lambdaQueryWrapper.like(${name}::getSearchStr, "%" + searchStr + "%");
		}
<#if hasSortColumn>
		lambdaQueryWrapper.orderByAsc(${name}::get${sortColumnName?cap_first});
</#if>
		IPage<${name}> pb = ${name?uncap_first}Dao.page(
			new Page<>(page, pageSize),
			lambdaQueryWrapper);
		return R.success(pb);
	}
}
